//
//  XMGPageView.m
//  09-分页功能01-
//
//  Created by xiaomage on 16/1/27.
//  Copyright © 2016年 小码哥. All rights reserved.
//

#import "XYPageView.h"

@interface XYPageView () <UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;

@property (weak, nonatomic) NSTimer *timer;
@end

@implementation XYPageView

- (void)awakeFromNib
{
    // 单页的时候是否隐藏pageControl
    self.pageControl.hidesForSinglePage = YES;

#pragma mark - 设置pageControl图片(KVC)
    [self.pageControl setValue:[UIImage imageNamed:@"current"] forKeyPath:@"_currentPageImage"];
    
    [self.pageControl setValue:[UIImage imageNamed:@"other"] forKeyPath:@"_pageImage"];
    
    // 开启定时器
    [self startTimer];
}

+ (instancetype)pageView
{
    //NSStringFromClass(self) == @"XMGPageView"
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
}

- (void)setImageNames:(NSArray *)imageNames
{
    _imageNames = imageNames;
    
    // 0.移除之前添加的ImageView
//    for (UIImageView *imageView in self.scrollView.subviews) {
//        [imageView removeFromSuperview];
//    }
    // 让subviews这个数组中每一个对象都执行removeFromSuperview
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    // 1.根据图片名数据创建imageView添加scrollView
    CGFloat scrollViewW = self.scrollView.frame.size.width;
    CGFloat scrollViewH = self.scrollView.frame.size.height;
    for (int i = 0; i < imageNames.count; i ++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.image = [UIImage imageNamed:imageNames[i]];
        imageView.frame = CGRectMake(i * scrollViewW, 0, scrollViewW, scrollViewH);
        [self.scrollView addSubview:imageView];
    }
    
    // 2.设置contentSize
    self.scrollView.contentSize = CGSizeMake(imageNames.count * scrollViewW, 0);
    
    // 3.设置总页数
    self.pageControl.numberOfPages = imageNames.count;
    
}

#pragma mark - 定时器相关的方法
- (void)startTimer
{
    // 创建一个自动执行任务的定时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextPage:) userInfo:@"123" repeats:YES];
    
    // NSDefaultRunLoopMode(默认):同一时间只能执行一个任务
    // NSRunLoopCommonModes(通用):可以分配一定的时间处理其他任务
    
    // 作用:修改timer在runLoop中的模式为NSRunLoopCommonModes;
    // 目的:不管主线程在做什么操作,都会分配一定的时间处理timer
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)stopTimer
{
    [self.timer invalidate];
}
/**
 *  滚动到下一页
 */
- (void)nextPage:(NSTimer *)timer
{
    
    //    NSLog(@"nextPage--%@",timer.userInfo);
    // 计算下一页的页码
    NSInteger page = self.pageControl.currentPage + 1;
    
    // 超过最后一页
    if (page == self.imageNames.count) {
        page = 0;
    }
    
    // 滚动到下一页
    [self.scrollView setContentOffset:CGPointMake(page * self.scrollView.frame.size.width, 0) animated:YES];
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 计算页码
    int page = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5);
    
    // 设置页码
    self.pageControl.currentPage = page;
}

// 用户开始拖拽scrollView时,停止定时器
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self stopTimer];
}

// 用户停止拖拽scrollView时,开启定时器
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self startTimer];
}

@end
